<template>
    <view class="login" >
        
      
      
         
        
        <view class="getPhoneNumber-wrap" v-if="isShowGetUserPhone">
                <view  class="getPhoneNumber-box">
                    <view class="title">微信授权</view>
                    <view class="icon"><image :src="require('./_static/images/login_icon.png')" /></view>
                    <view class="txt">微信授权成功</view>
                    
                    <view class="desc">授权绑定你的手机号</view>    
                    <view class="btns">
                       <button type='primary' class="cannel" lang="zh_CN" @tap="cannel">拒绝</button>
                       <button type='primary' open-type="getPhoneNumber" lang="zh_CN" @getphonenumber="getPhoneNumber">微信登录</button>
                    </view>     
                </view>
      
        </view>
        <view class="getuserinfo-wrap" wx:else>

           <image :src="require('./_static/images/login_logo.jpg')" class="logo" /> 


            <button
            
         
            class="getuserinfo-cp" 
            open-type="getUserInfo"
            lang="zh_CN" 
            @getuserinfo="getuserinfo">一键登录</button>


            <view class="login-rule" >
                
                        我已阅读并同意  <text @tap="toStatic('10')">"服务协议"</text>和<text  @tap="toStatic('11')">"隐私协议"</text>
                
              </view>

        </view>
      



    </view>
</template>
<script>


import { postlogin,postgetUserInfo ,postGetCartList,postGetPhone} from '@/request/actions'



export default  {
  
    data(){
        return{
            checked:true,
            mobile:'',
            isShowGetUserPhone:false,
            query:{},
            disabledUserPhone:false,
            disabledUserInfo:false,
            userInfo:{},
            code:''
        }
        
       
    },

    methods:{
         
            toRoute(){
                    let query = this.query;
                    if(Object.keys(query).length == 0){
                        return uni.navigateBack();
                    }else{
                        let url = query['form'] + '?';
                        Object.keys(query).forEach(k=>{
                                if(k !== 'form'){
                                    url += k + '=' + query[k] + '&'
                                }
                            
                        })
                            
                        url = url.substr(0,url.length-1);
                
                        return uni.reLaunch({url:'/'+url})

                    }
            
            },
              saveUseInfo(d){
                    let globalData = getApp().globalData;
                    globalData.userInfo = d;
                    console.log(getApp())
                    wx.setStorageSync('userInfo',JSON.stringify(d));
              },
    
        toStatic(id){
            uni.navigateTo({
                url:`/detailPackages/pages/agree/index?id=${id}`
            })
        },
        cannel(){
            this.disabledUserInfo = false;
            this.isShowGetUserPhone = false;
            uni.login({
                success:res=>{
                     if(res.code){
                         
                         this.code = res.code;
                     }
                 }
              })
        },
        checkboxChange(e){
           this.checked = !this.checked;
          
        },
        getTel(e){
                 this.mobile = e.detail.value;
            },
     
       async getuserinfo(e){

                    let globalData = getApp().globalData;
                    let e_getUserInfo = e;
                    let that = this;
                    let code = this.code;
                    let res_getUserInfo = null;
                    let {appId} =globalData;
                    let { query } = globalData;    
                    if(this.disabledUserInfo)return;

                  

                    if(e_getUserInfo.detail.errMsg == 'getUserInfo:ok'){
                            uni.showLoading();
                            let {encryptedData,iv} = e_getUserInfo.detail;
                            let { 
                                 nickName:userName,
                                 gender:userSex,
                                 country:userCountry,
                                 province: userProvince,    
                                 city:userAddressid  ,
                                 avatarUrl:userHeadportrait,
                                } = e_getUserInfo.detail.userInfo;
                           
                    
                           let r = await postgetUserInfo({...query,userHeadportrait, code,appId,userName,userSex,userCountry,userProvince,userAddressid});
                           res_getUserInfo = r.data;
                          
                           this.disabledUserInfo = true;
                         
                        
                   
                   }else{
                           return ;
                    }
                  
       
                  
                    if(res_getUserInfo.status == 200){
                       this.userInfo = {...res_getUserInfo.data,...e_getUserInfo.detail.userInfo};
                       let {userPhone} = this.userInfo;
                      
                        
                       if(userPhone){
                            
                             uni.showToast({
                                title:'登录成功',
                                icon: 'none',
                                duration: 1000 
                            })

                           return  setTimeout(()=>{
                                this.saveUseInfo(this.userInfo);
                               
                                this.toRoute()
                                
                               
                            },300)
                           
                       }else{
                            this.isShowGetUserPhone = true;
                       }

                       
      
                    }else{

                         uni.showToast({
                                title:'获取用户信息失败',
                                icon: 'none',
                                duration: 2000 
                            })
                    }
                           
                     this.disabledUserInfo = false;
                      
                     uni.hideLoading();  
            
      
            

        
           
        },
        async getPhoneNumber(e){
                let globalData = getApp().globalData;
                 let e_getPhoneNumber = e;
                 let res_getPhoneNumber = null;
                 let {appId} = globalData;
                 let that = this;
                 this.isShowGetUserPhone = false;
                 
                 if(this.disabledUserPhone)return;

                  if(e_getPhoneNumber.detail.errMsg == 'getPhoneNumber:ok'){
                            let {encryptedData,iv} = e_getPhoneNumber.detail;
                            let userInfo =  this.userInfo;
                            let {session_key,userId} = userInfo;
                             this.disabledUserPhone = true;   
                             
                          
                             let r = await postGetPhone({encryptedData,iv,appId,session_key,userId});
                             res_getPhoneNumber = r.data;

                           
                            if(res_getPhoneNumber.status == 200){
                                let userPhone = res_getPhoneNumber.data;
                                this.userInfo = {...userInfo,userPhone}
                                this.saveUseInfo(this.userInfo );

                          
                                     uni.showModal({
                                        title:'登录成功',
                                        showCancel:false,
                                        success:()=>{
                                            
                                                return this.toRoute();
                                        }
                                    })
                              
                               
                                
                             
                            }else{

                                 uni.showToast({
                                        title:'获取用户手机失败',
                                        icon: 'none',
                                        duration: 2000 
                                    })
                            }

                            

                            


                            
                    }else{
                          
                          return  uni.navigateBack();
                    }
        }
    },

    
    onShow(){
    
        this.isShowGetUserPhone = false;
        this.disabledUserInfo = false;
 
           uni.login({
                success:res=>{
                    console.log(res)
                     if(res.code){
                         
                         this.code = res.code;
                     }
                 }
              })
    },
    onLoad(query){
        this.query = query;
  
                   
                  

    }
    
}

</script>

<style lang="scss">
@import './_static/scss/index.scss';
  
</style>